const {
	Controller
} = require('egg');
const tencentcloud = require("tencentcloud-sdk-nodejs-nlp");
const tencentcloud1 = require("tencentcloud-sdk-nodejs-fmu");
const tencentcloud2 = require("tencentcloud-sdk-nodejs-aiart");
const fs = require("fs")
const path = require("path");
const { log } = require('console');
class HomeController extends Controller {
	async index() {
	//给请求的客户端发送数据
		this.ctx.body = 'hi, egg';
	}
	
async setpic(){
	let fdata = this.ctx.request.files
	let {type} = this.ctx.request.body
	console.log(fdata);
	let oldpath=fdata[0].filepath
	let basename = path.basename(oldpath)
	let img = "http://127.0.0.1:7001/public/upload/" + basename
	//__dirname 是一个字符串，代表当前目录
	let newpath=__dirname + "/../public/upload/" + basename
	//renameFileSync 重命名+移动 copyFileSync 拷贝
	fs.copyFileSync(oldpath,newpath)
	let sql = `INSERT INTO bizi (img,fenlei) values ("${img}","${type}")`
	
	this.ctx.body = "Stablity Above All"
	let dbres = await this.ctx.app.mysql.query(sql)
	console.log(dbres);
}
async changepic(){
	let f = this.ctx.request.files
	console.log(f[0].filepath);
	//读取计算机中的图像(客户端发送)
	let fdata =await fs.readFileSync(f[0].filepath)
	let base64img = "data:image/png;base64," + fdata.toString("base64")
	console.log(base64img);
	// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
	const tencentcloud = require("tencentcloud-sdk-nodejs-aiart");
	
	const AiartClient = tencentcloud.aiart.v20221229.Client;
	
	// 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
	// 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
	// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
	const clientConfig = {
	  credential: {
		secretId: "AKIDkOyQh4Y3MRUco6Ss9Qcr3JUeiaGoDWjo",
		secretKey: "0thUBmIhJqZRPExcR5Z6fAjDXr491mLn",
	  },
	  region: "ap-guangzhou",
	  profile: {
	    httpProfile: {
	      endpoint: "aiart.ap-chengdu.tencentcloudapi.com",
	    },
	  },
	};
	
	// 实例化要请求产品的client对象,clientProfile是可选的
	const client = new AiartClient(clientConfig);
	const params = {
	    "InputImage": base64img,
	    "InputUrl": "",
	    "Styles": [
	        "201"
	    ],
	    "LogoAdd": 0,
	    "RspImgType": "url"
	};
	let changepicture= await client.ImageToImage(params)
	console.log(changepicture);
	this.ctx.body = {url:changepicture.ResultImage}
}
}
module.exports = HomeController;